#!/bin/bash
#
# clamav-cron v. 0.7 - Copyright © 2009, Stefano Stagnaro
# This is Free Software released under the GNU GPL license version 3

#============================================#
#        User configuration section          #
#============================================#

# Notification e-mail sender (could be fake):
CV_MAILFROM="clamav@aka.net"

# Notification e-mail recipient:
CV_MAILTO="paolo@aka.net"

# Notification e-mail secondary recipients:
#CV_MAILTO_CC="stefano@aka.net, danilo@aka.net"

# Notification e-mail subject:
CV_SUBJECT="AKA Corp. - ClamAV scan report"

# Log file name and its path:
CV_LOGFILE="/var/log/clamav/clamav-cron.log"

#============================================#

CV_TARGET="$1"
CV_VERSION="0.7"

if [ -e $CV_LOGFILE ]
then
	/bin/rm $CV_LOGFILE
fi

if [ -z "$1" ]
then
	CV_TARGET="$HOME"
fi

#To be read on stdout (and root mail):
echo -e clamav-cron v. $CV_VERSION - Copyright 2009, Stefano Stagnaro '\n'

#To be read on logfile (sent via sendmail):
echo -e $CV_SUBJECT - $(date) '\n' >> $CV_LOGFILE
echo -e Script: clamav-cron v. $CV_VERSION - Copyright 2009, Stefano Stagnaro  >> $CV_LOGFILE
echo -e Scanned: $CV_TARGET on $HOSTNAME'\n' >> $CV_LOGFILE

/usr/bin/freshclam --log=$CV_LOGFILE --user $USER --no-warnings 

#To be read on stdout (and root mail):
echo -e '------------------------------------\n'

/usr/bin/clamscan --infected --log=$CV_LOGFILE --recursive $CV_TARGET
CLAMSCAN=$?

if [ "$CLAMSCAN" -eq "1" ]
then
	CV_SUBJECT="[VIRUS!] "$CV_SUBJECT
elif [ "$CLAMSCAN" -gt "1" ]
then
	CV_SUBJECT="[ERROR] "$CV_SUBJECT
fi

if [ -z "$CV_MAILTO_CC" ]
then
	/bin/mail -s "$CV_SUBJECT" $CV_MAILTO -- -f $CV_MAILFROM < $CV_LOGFILE
else
	/bin/mail -s "$CV_SUBJECT" -c "$CV_MAILTO_CC" $CV_MAILTO -- -f $CV_MAILFROM < $CV_LOGFILE
fi
